﻿using System;
using System.Data.SqlServerCe;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SenseTimeTracker.Misc;
using SenseTimeTracker.Properties;
using StedySoft.SenseSDK;
using StedySoft.SenseSDK.DrawingCE;

namespace SenseTimeTracker.Users
{
	public partial class PasswordForm : CommonSenseControls.CommonSenseForm
	{
		/// <summary>
		/// Field contains the user name
		/// </summary>
		private string userName;

		private SensePanelItem userNameItem;
		private SensePanelTextboxItem userPasswordItem;

		/// <summary>
		/// Initializes a new instance of the <see cref="PasswordForm"/> class.
		/// </summary>
		public PasswordForm()
		{
			InitializeComponent();

			senseHeaderCtrl.Text = Resources.PasswordHeader;
			senseListCtrl.AddItem(new SensePanelDividerItem(string.Empty, Resources.PasswordExplanation));

			userNameItem = new SensePanelItem();
			userNameItem.Name = string.Format("{0}:", Resources.NameLabel);
			userNameItem.ShowSeparator = true;
			senseListCtrl.AddItem(userNameItem);

			userPasswordItem = new SensePanelTextboxItem();
			userPasswordItem.LabelText = string.Format("{0}:", Resources.PasswordLabel);
			userPasswordItem.Text = string.Empty;
			userPasswordItem.PasswordChar = '*';
			userPasswordItem.MaxLength = DatabaseManager.MaxUserPasswordLength;
			userPasswordItem.ShowSeparator = true;
			senseListCtrl.AddItem(userPasswordItem);

			SIP.Enable(senseListCtrl.Handle);
		}

		/// <summary>
		/// Gets or sets the user name
		/// </summary>
		public string UserName
		{
			get { return userName; }
			set { userName = value; }
		}

		protected override void OnLoad(EventArgs e)
		{
			userNameItem.PrimaryText = userName;

			base.OnLoad(e);
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			if (DialogResult != DialogResult.OK)
			{
				return;
			}

			if (userPasswordItem.Text == string.Empty)
			{
				SenseAPIs.SenseMessageBox.Show(Resources.DialogMessageHaveToEnterPassword, Resources.DialogHeaderPassword, SenseMessageBoxButtons.OK);
				e.Cancel = true;
				return;
			}

			SqlCeCommand command = new SqlCeCommand(
				"SELECT personId, personPassword " +
				"FROM Persons " +
				"WHERE personName = @personName",
				DatabaseManager.DbConnection);
			command.Parameters.Add("@personName", UserName);
			DatabaseManager.DbConnection.Open();
			SqlCeDataReader reader = command.ExecuteReader();
			while (reader.Read())
			{
				if (reader["personPassword"].ToString() == Password.Encrypt(userPasswordItem.Text))
				{
					DatabaseManager.DbConnection.Close();
					return;
				}
			}

			DatabaseManager.DbConnection.Close();
			SenseAPIs.SenseMessageBox.Show(Resources.DialogMessageWrongPassword, Resources.DialogHeaderPassword, SenseMessageBoxButtons.OK);
			e.Cancel = true;
			base.OnClosing(e);
		}
	}
}